package main;

use XML::Simple;
use LWP::Simple;
use Data::Dumper;
use File::Slurp;


use Carp;

sub open_data_file
{
   my $xml = new XML::Simple;
   my $status = "nothing yet";
   my $textsource = get "http://einskanal.net/data.xml" and $status = "remote file";
   $_[0] and $textsource=$_[0] and $status ="custom file";
   $status eq /nothing yet/ and $textsource = "data.xml"; #default file

   my $data = $xml->XMLin("inter.xml", forcearray=>['phrase','state','transition'], keyattr=>['name']);

   return $data;
}

sub load
{
   my $xml = new XML::Simple;
   my $status = "nothing yet";
   my $file = shift;

   my $data = $xml->XMLin($file, forcearray=>['character']) or die "data source not found ._.\n";

   return $data;
}

sub errorlog
{
   my $file = "errors.txt";
   open(LOG,'>>'.$file);
   print LOG $_;
}

sub save
{
   my $xml = new XML::Simple;
   my $filename = shift;
   my ($data) = @_;
   my $xmlstuff = $xml->XMLout($data) or die "couldnt write ._.\n";
   write_file $filename, $xmlstuff;
                                                                  
  return 1;

}
;1
